package com.jltx.xq.simplewatch.base.presenter;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/18.
 */

public abstract class BasePresenter<V> {
    public V mView;
    private CompositeDisposable compositeDisposable;  //统一管理RxJava2的线程，防止内存泄漏

    public BasePresenter(V view) {
        attachView(view);
    }

    public void attachView(V view) {
        mView = view;
    }

    public boolean isViewAttached() {
        return mView != null;
    }

    public void detachView() {
        mView = null;
        unSubscribe();
    }

    public void addDisposable(Disposable disposable){
        if(compositeDisposable == null){
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }

    public void unSubscribe(){
        if(compositeDisposable != null){
            compositeDisposable.clear();
        }
    }

}
